package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.baseController.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.Lease;
import com.dc.model.User;
import com.dc.service.LeaseService;
import com.dc.service.UserService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(value="租出模块")
@RequestMapping("/lease")
@Controller
public class LeaseController extends BaseController {
    @Autowired
    LeaseService leaseService;

    // 分页查询租出信息
    // 新增日志信息
    @AopOperation(desc = "分页查询租出信息",type = "查询",menu = RoleMenuEnum.NO_201)
    @ApiOperation(value="分页查询租出信息",httpMethod = "GET",notes = "查询")
    @RequestMapping(value = "/selectPageLease", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectPageLease(
            @RequestParam(value="curr_page",defaultValue = "1") int curr_page,
            @RequestParam(value="page_size",defaultValue = "10") int page_size,
            BaseModel baseModel,
            @RequestParam(value="lease_id",required = false) Integer lease_id,
            @RequestParam(value="client_name",required = false)String client_name,
            @RequestParam(value="flower_name",required = false)String flower_name){
        return  leaseService.selectPageLease(curr_page,page_size,baseModel,lease_id,client_name,flower_name);

    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "新增租出",type="新增",menu = RoleMenuEnum.NO_201)
    // 新增租出信息
    @ApiOperation(value="租出新增",httpMethod = "POST",notes="新增")
    @RequestMapping(value="/addLease", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="到期时间",name="expiration_dates",required = true,dataType = "String",paramType = "query")
    })
    public BaseModel addLease(Lease lease,String expiration_dates, HttpServletRequest request, BaseModel baseModel)throws Exception{
        lease.setCreate_time(new Date());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(expiration_dates);
        lease.setExpiration_date(date);
        /* 获取session,判断是否登录 */
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser==null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return leaseService.addLease(lease,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "修改租出信息",type="修改",menu = RoleMenuEnum.NO_201)
    // 修改租出信息
    @ApiOperation(value="租出修改",httpMethod = "POST",notes="修改")
    @RequestMapping(value="/updateLease", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="到期时间",name="expiration_dates",required = true,dataType = "String",paramType = "query")
    })
    public BaseModel updateLease(Lease lease,String expiration_dates,BaseModel baseModel,HttpServletRequest request)throws Exception {
        lease.setUpdate_time(new Date());
        if(expiration_dates!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(expiration_dates);
            lease.setExpiration_date(date);
        }
        // 获取登录信息
        HttpSession session=request.getSession();
        // 记住要用User
        User currentUser=(User) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return leaseService.updateLease(lease,baseModel);
    }

    // 根据id查询租出信息
    @ApiOperation(value="租出查询",httpMethod = "GET",notes="查询")
    @RequestMapping(value="/selectLeaseById", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectLeaseById(@RequestParam("lease_id")Integer lease_id,
                                      BaseModel baseModel,
                                      HttpServletRequest request)throws Exception{
        return leaseService.selectLeaseById(lease_id,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "删除租出",type="删除",menu = RoleMenuEnum.NO_201)
    // 删除租出信息(修改)
    @ApiOperation(value="租出删除",httpMethod = "POST",notes="删除")
    @RequestMapping(value="/deleteLease/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel deleteLease(@PathVariable(value="ids") String ids,
                                  BaseModel baseModel,
                                  HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        User currentUser = (User)session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return leaseService.deleteLease(ids,baseModel);
    }
}
